﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace DemoTest
{
    public partial class XMLtest : Form
    {
        public XMLtest()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //创建类型声明节点  
            XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");                   
            xmlDoc.AppendChild(node);



            //XmlNode root1 = xmlDoc.CreateNode("business");
            //root1.SetAttribute("comment", "发票开具");
            //root1.SetAttribute("id", "FPKJ");
            //xmlDoc.AppendChild(root1);

            //XmlElement root2 = xmlDoc.CreateElement("body");
            //root2.SetAttribute("yylxdm", "1");
            //xmlDoc.AppendChild(root2);


            //创建根节点  
            XmlElement roota = xmlDoc.CreateElement("bussiness");
            roota.SetAttribute("comment","发票开具");
            roota.SetAttribute("id", "FPKJ");
            xmlDoc.AppendChild(roota);

            XmlElement passElement = xmlDoc.CreateElement("body");
            passElement.SetAttribute("yylxdm", "1");
            //将新创建的节点pass添加为person节点的子节点  
            xmlDoc.DocumentElement.AppendChild(passElement);


            XmlNode node1 = xmlDoc.CreateNode(XmlNodeType.Element, "User", null);
            CreateNode(xmlDoc, node1, "name", "xuwei");
            CreateNode(xmlDoc, node1, "sex", "male");
            CreateNode(xmlDoc, node1, "age", "25");
            roota.AppendChild(node1);

            XmlNode node2 = xmlDoc.CreateNode(XmlNodeType.Element, "User", null);
            CreateNode(xmlDoc, node2, "name", "xiaolai");
            CreateNode(xmlDoc, node2, "sex", "female");
            CreateNode(xmlDoc, node2, "age", "23");
            roota.AppendChild(node2);


            try
            {
                xmlDoc.Save("d://data5.xml");
            }
            catch (Exception ex)
            {
                //显示错误信息  
                Console.WriteLine(ex.Message);
            }
            //Console.ReadLine();  

        }
        public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }
        public XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
        {
            try
            {
                XmlDocument doc = node.OwnerDocument;
                XmlAttribute attr = null;
                attr = doc.CreateAttribute(attributeName);
                attr.Value = value;
                node.Attributes.SetNamedItem(attr);
                return attr;
            }
            catch (Exception err)
            {
                string desc = err.Message;
                return null;
            }
        }
    }
}
